package io.renren.sdapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import io.renren.sdapi.dto.httprequestparam.SseMessageDto;
import io.renren.sdapi.service.SseService;
import io.renren.util.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/sse")
public class SseController {

    @Autowired
    private SseService sseService;

    /**
     * 首页
     *
     * @return
     */
    @GetMapping("/")
    public String index() {
        return "index";
    }

    /**
     * 创建SSE连接
     *
     * @return
     */
    //@SaCheckLogin
    //@GetMapping(path = "/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @GetMapping("/connect")
    public SseEmitter sse() {
        String uuid = UUID.randomUUID().toString();
        log.info("新用户连接：{}", uuid);
        return sseService.connect(uuid);
    }

//    /**
//     * 广播消息,后端测试调用
//     *
//     * @param message
//     */
//    @PostMapping("/sendMessage")
//    public void sendMessage() {
//        SseMessageDto message = new SseMessageDto();
//
//        sseService.sendMessage(message);
//        return BaseResponse.success();
//    }
}
